package com.sale.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sale.common.core.exception.GlobalException;
import com.sale.common.core.utils.BeanCopyUtils;
import com.sale.common.core.utils.StringUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.RedisUtils;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.system.domain.*;
import com.sale.system.domain.dto.MaterialRawDto;
import com.sale.system.domain.dto.SysMaterialCodingDto;
import com.sale.system.domain.dto.SysTheDropDto;
import com.sale.system.domain.vo.SysMaterialCodingMqVo;
import com.sale.system.domain.vo.SysMaterialCodingVo;
import com.sale.system.mapper.*;
import com.sale.system.service.ISysMaterialCodingService;
import com.sale.system.util.MaterialMqUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: YSL
 * @Date: 2023-07-10 09:24:21
 * @Desc: 物料类别表
 */
@RequiredArgsConstructor
@Service
public class SysMaterialCodingServiceImpl implements ISysMaterialCodingService {

    /**
     * 物料类别Mapper
     */
    private final SysMaterialCodingMapper sysMaterialCodingMapper;

    /**
     * 成品mapper
     */
    private final MaterialFinishedMapper materialFinishedMapper;

    /**
     * 原料mapper
     */
    private final MaterialRawMapper materialRawMapper;

    /**
     * 半成品mapper
     */
    private final MaterialHalfMapper materialHalfMapper;

    /**
     * 辅料mapper
     */
    private final MaterialAccessoriesMapper materialAccessoriesMapper;

    /**
     * 废料mapper
     */
    private final MaterialWasteMapper materialWasteMapper;

    /**
     * 类别表mapper
     */
    private final SysCategoryMapper sysCategoryMapper;

    /**
     * 查询物料类别列表
     *
     * @param sysMaterialCodingVo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<SysMaterialCodingDto> selectSysMaterialCodingList(SysMaterialCodingVo sysMaterialCodingVo, PageQuery pageQuery) {
        // 分页
        Page<SysMaterialCodingDto> sysMaterialCodingPage = sysMaterialCodingMapper.selectSysMaterialCodingList(pageQuery.build(), sysMaterialCodingVo);
        return TableDataInfo.build(sysMaterialCodingPage);
    }

    /**
     * 根据物料类别查询详情
     *
     * @param codingId
     * @return
     */
    @Override
    public SysMaterialCoding getSysMaterialCodingById(Long codingId) {
        return sysMaterialCodingMapper.selectById(codingId);
    }


    /**
     * 新增物料类别
     *
     * @param sysMaterialCodingVo
     * @return
     */
    @Override
    public int insertMaterialCoding(SysMaterialCodingVo sysMaterialCodingVo) throws Exception {
        SysMaterialCoding sysMaterialCoding = new SysMaterialCoding();
        //校验编码是否唯一
        SysMaterialCoding sysMaterialCoding1 = sysMaterialCodingMapper.selectOne(new QueryWrapper<SysMaterialCoding>().lambda()
            .eq(SysMaterialCoding::getCodingCode, sysMaterialCodingVo.getCodingCode()));
        if (sysMaterialCoding1 != null) {
            throw new GlobalException("该编码已存在");
        }
        BeanUtils.copyProperties(sysMaterialCodingVo, sysMaterialCoding);
        int insert = sysMaterialCodingMapper.insert(sysMaterialCoding);
        //发送mq
        SysMaterialCodingMqVo sysMaterialCodingMqVo = new SysMaterialCodingMqVo();
        BeanUtils.copyProperties(sysMaterialCoding, sysMaterialCodingMqVo);
        //租户id
        String userIdStr = LoginHelper.getUserIdStr();
        String tenantId = RedisUtils.getCacheObject(userIdStr);
        sysMaterialCodingMqVo.setTenantId(Long.parseLong(tenantId));
        sysMaterialCodingMqVo.setCodingId(sysMaterialCoding.getCodingId());
        MaterialMqUtil.materialCodingMqSend(sysMaterialCodingMqVo);
        return insert;
    }

    /**
     * 修改物料类别
     *
     * @param sysMaterialCodingVo
     * @return
     */
    @Override
    public int updateMaterialCoding(SysMaterialCodingVo sysMaterialCodingVo) throws Exception {
        SysMaterialCoding sysMaterialCoding = new SysMaterialCoding();
        BeanUtils.copyProperties(sysMaterialCodingVo, sysMaterialCoding);
        //发送mq
        SysMaterialCodingMqVo sysMaterialCodingMqVo = new SysMaterialCodingMqVo();
        BeanUtils.copyProperties(sysMaterialCoding, sysMaterialCodingMqVo);
        MaterialMqUtil.materialCodingMqSend(sysMaterialCodingMqVo);
        return sysMaterialCodingMapper.updateById(sysMaterialCoding);
    }

    /**
     * 删除物料类别
     *
     * @param codingId
     * @return
     */
    @Override
    public int deleteMaterialCoding(Long codingId) throws Exception {
        //发送mq
        SysMaterialCodingMqVo sysMaterialCodingMqVo = new SysMaterialCodingMqVo();
        sysMaterialCodingMqVo.setIdStr(String.valueOf(codingId));
        MaterialMqUtil.materialCodingMqSendDelete(sysMaterialCodingMqVo);
        return sysMaterialCodingMapper.deleteById(codingId);
    }

    /**
     * 物料类别下拉
     *
     * @return
     */
    @Override
    public List<SysTheDropDto> selectDropdown(SysMaterialCodingVo sysMaterialCodingVo) {
        return sysMaterialCodingMapper.selectDropdown(sysMaterialCodingVo);
    }

    /**
     * 根据物料编码获取对应类别信息（dubbo 调用）
     *
     * @param materialCode
     * @return
     */
    @Override
    public SysCategory getByCode(String materialCode) {
        //先判断该编码是原料、成品、半成品、辅料、废料
        MaterialFinished materialFinished = materialFinishedMapper.selectMaterialFinishedMaterialCode(materialCode);
        //原料
        MaterialRawDto materialRawDto = materialRawMapper.selectDataByCode(materialCode);
        //半成品
        MaterialHalf materialHalf = materialHalfMapper.selectOne(new QueryWrapper<MaterialHalf>()
            .lambda().eq(MaterialHalf::getMaterialCode, materialCode));
        //辅料
        MaterialAccessories materialAccessories = materialAccessoriesMapper.selectOne(new QueryWrapper<MaterialAccessories>()
            .lambda().eq(MaterialAccessories::getMaterialCode, materialCode));
        //废料
        MaterialWaste materialWaste = materialWasteMapper.selectOne(new QueryWrapper<MaterialWaste>()
            .lambda().eq(MaterialWaste::getMaterialCode, materialCode));

        if (materialFinished != null) {
            String materialType = String.valueOf(materialFinished.getMaterialType());
            return sysCategoryMapper.selectById(materialType);
        } else if (materialRawDto != null) {
            long materialType = materialRawDto.getMaterialType();
            return sysCategoryMapper.selectById(materialType);
        } else if (materialHalf != null) {
            long materialType = materialHalf.getMaterialType();
            return sysCategoryMapper.selectById(materialType);
        } else if (materialAccessories != null) {
            Long materialType = Long.parseLong(materialAccessories.getMaterialType());
            return sysCategoryMapper.selectById(materialType);
        } else {
            long materialType = materialWaste.getMaterialType();
            return sysCategoryMapper.selectById(materialType);
        }
    }

    /**
     * 获取全部物料类别下拉
     */
    @Override
    public List<SysTheDropDto> selectAllDropdown() {
        return sysMaterialCodingMapper.selectAllDropdown();
    }


}
